/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


package database.daos;

import database.orm.ORMException;
import database.databasemanager.exceptions.DataBaseException;
import beans.StoreBean;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author Fam. Tapia
 */
public class StoreDAO extends AbstractDAO<StoreBean>{


    @Override
    public void insert(StoreBean store) throws DAOException {
         try {
            dataBaseManager.openConnection();
            String sqlStatement = "INSERT INTO STORE(storeName,storeBranch,storeRole,storeRFC,storeLocalNumber,storePhone,storeAddress )values('" + store.getStoreName() + "','" + store.getStoreBranch() + "','" + store.getStoreRole() + "','" + store.getStoreRFC() + "','" + store.getStoreLocalNumber() + "','" + store.getStorePhone() + "','" + store.getStoreAddress() + "');";
            dataBaseManager.executeUpdate(sqlStatement);
        }catch (DataBaseException exception) {
            throw new DAOException(exception.getExceptionMessage(),exception);
        } finally{
            try {
                    dataBaseManager.closeConnection();
            } catch (DataBaseException exception) {
                    throw new DAOException(exception.getExceptionMessage(),exception);
            }
        }
    }

   
    @Override
    public void delete(String condition) throws DAOException {
        try {
                dataBaseManager.openConnection();
                String sql = "DELETE FROM store " + condition;
                dataBaseManager.executeUpdate(sql);
        }catch (DataBaseException exception) {
            throw new DAOException(exception.getExceptionMessage(),exception);
        } finally{
            try {
                    dataBaseManager.closeConnection();
            } catch (DataBaseException exception) {
                    throw new DAOException(exception.getExceptionMessage(),exception);
            }
        }
    }

    @Override
    public void update(StoreBean store, String condition) throws DAOException {
        try {
                dataBaseManager.openConnection();
                String sql = "UPDATE store SET storeId='" + store.getStoreId() + "',storeName='" + store.getStoreName() + "',storeBranch='" + store.getStoreBranch() + "',storeRole='" + store.getStoreRole() + "',storeRFC='" + store.getStoreRFC() + "',storeLocalNumber='" + store.getStoreLocalNumber() + "',storePhone='" + store.getStorePhone() + "',storeAddress='" + store.getStoreAddress() + "' " + condition;
                dataBaseManager.executeUpdate(sql);
        }catch (DataBaseException exception) {
            throw new DAOException(exception.getExceptionMessage(),exception);
        } finally{
            try {
                    dataBaseManager.closeConnection();
            } catch (DataBaseException exception) {
                    throw new DAOException(exception.getExceptionMessage(),exception);
            }
        }
    }

    @Override
    protected ArrayList<StoreBean> searchAll(String condition) throws DAOException {
        try {
            dataBaseManager.openConnection();
            String sqlStatement = "SELECT * FROM STORE " + condition;
            ArrayList<StoreBean> storeBeanlist = null;
            storeBeanlist = createStoreBeanList(dataBaseManager.executeQuery(sqlStatement));
            return storeBeanlist;
        }catch (DataBaseException exception) {
            throw new DAOException(exception.getExceptionMessage(),exception);
        } finally{
            try {
                    dataBaseManager.closeConnection();
            } catch (DataBaseException exception) {
                    throw new DAOException(exception.getExceptionMessage(),exception);
            }
        } 
    }



    @Override
    protected StoreBean search(String condition) throws DAOException {
        try {
            dataBaseManager.openConnection();
            String sqlStatement = "SELECT * FROM STORE " + condition;
            StoreBean storeBean = null;
            storeBean = createStoreBean(dataBaseManager.executeQuery(sqlStatement));
        return storeBean;
        }catch (DataBaseException exception) {
                throw new DAOException(exception.getExceptionMessage(),exception);
        } finally{
             try {
                        dataBaseManager.closeConnection();
             } catch (DataBaseException exception) {
                        throw new DAOException(exception.getExceptionMessage(),exception);
             }
        }
    }



 private ArrayList<StoreBean> createStoreBeanList(ResultSet queryResult) throws DAOException{
        try {
            ArrayList<StoreBean> storeBeanList = new ArrayList();
            while (queryResult.next()) {
                storeBeanList.add((StoreBean) beansGenerator.generateBean(queryResult, StoreBean.class));
            }
            return storeBeanList;
        } catch (SQLException exception) {
            throw new DAOException(exception.getMessage(),exception);
        } catch (ORMException exception){
                throw new DAOException(exception.getExceptionMessage(),exception);
        }
    }

    private StoreBean createStoreBean(ResultSet queryResult) throws DAOException{
        try{
            moveQueryResultToBegin(queryResult);
            return (StoreBean) beansGenerator.generateBean(queryResult, StoreBean.class);
         } catch (SQLException exception) {
            throw new DAOException(exception.getMessage(),exception);
        } catch (ORMException exception){
                throw new DAOException(exception.getExceptionMessage(),exception);
        }
    }


    public void deleteStore(String RFC) throws DAOException{
            delete("WHERE storeRFC='"+RFC+"';");
    }

    public StoreBean searchStore(String RFC) throws DAOException{
        return search("WHERE storeRFC='"+RFC+"';");
    }

     public void moveQueryResultToBegin(ResultSet queryResult) throws SQLException{
        queryResult.first();
    }


}
